<?php
/**
* //////////////////////////////////////////////////
* auto execute , for initialize config variable
* this function auto call every page has been fetch
*/
function VoteThailandPoll_permission()
{
  // Security check
  //we are allow for admin access level , see in config.php variable name 800
  if (!SecurityUtil::checkPermission('VoteThailandPoll::', '::', ACCESS_ADMIN)) {
      LogUtil::registerPermissionError(pnModUrl('Users','user','loginscreen'));
  }
}

/**
* smarty template auto call before render
*/
function _preRender(&$render){
  $lang    = FormUtil::getPassedValue ('lang', false , 'GET');
  $ctrl    = FormUtil::getPassedValue ('ctrl', false , 'GET');

  $render->assign ('_GET', $_GET);
  $render->assign ('_POST', $_POST);
  $render->assign ('_REQUEST', $_REQUEST);

  $render->assign('ctrl', $ctrl);
  $render->assign('user',   InnoUtil::getUserInfo());

  if ($lang){
    $render->assign('lang', $lang);
  }else{
    $render->assign('lang', pnUserGetLang());
  }
  $render->assign('access_edit', true);
}
/**
 * Main user function, simply return the index page.
 * @author Chayakon PONGSIRI
 * @return string HTML string
 */

function VoteThailandPoll_admin_main() {
    VoteThailandPoll_permission();
    return VoteThailandPoll_admin_list();
}
/**
* display page with out class loader
*/
function VoteThailandPoll_admin_page() {
    VoteThailandPoll_permission();
    //$ctrl the class name
    $ctrl    = FormUtil::getPassedValue ('ctrl', 'home' , 'GET');
    //$method the method of request for edit or view enum[ view | form | delete | list | page]
    $func  = FormUtil::getPassedValue ('func', 'page' , 'GET');
    $render = pnRender::getInstance('VoteThailandPoll');
    _preRender($render);
    return $render->fetch('admin_'.$func.'_'.strtolower($ctrl).'.htm');

}
/**
* display page with class that extend Object 
*/
function VoteThailandPoll_admin_view() {
    VoteThailandPoll_permission();
    //$ctrl the class name
    $ctrl    = FormUtil::getPassedValue ('ctrl', false , 'GET');
    //$method the method of request for edit or view enum[ view | form | delete | list | page]
    $func  = FormUtil::getPassedValue ('func', 'view' , 'GET');
    //$lang enum[eng | jpn | tha]
    $lang    = FormUtil::getPassedValue ('lang', null , 'GET');
    //$id the id no if edit form
    $id      = FormUtil::getPassedValue ('id', null , 'GET');
    //pagnigation variable
    $filter  = FormUtil::getPassedValue ('filter', 0);
    $offset  = FormUtil::getPassedValue ('startnum', 0);
    $sort    = FormUtil::getPassedValue ('sort', '');
    $where   = '';

    $pagesize = pnModGetVar ('VoteThailandPoll', 'pagesize') ? pnModGetVar ('VoteThailandPoll', 'pagesize') : 100;
    $render = pnRender::getInstance('VoteThailandPoll');
    
    if ($id){
      //load class
      if (!($class = Loader::loadClassFromModule ('VoteThailandPoll', $ctrl, false)))
        return LogUtil::registerError ("Unable to load class [$ctrl] ...");

      $object  = new $class ();
      $object->get($id);
      if (method_exists($object,'selectExtendResult')){
        $resultex = $object->selectExtendResult();
        $render->assign('extendResult', $resultex);
      }
      $render->assign ('view', $object->_objData);
    }
    //_preRender($render);
    return $render->fetch('admin_'.$func.'_'.strtolower($ctrl).'.htm');
}

/**
* display page with class that extend Object Array
*/
function VoteThailandPoll_admin_form() {
    VoteThailandPoll_permission();
    //$ctrl the class name
    $ctrl    = FormUtil::getPassedValue ('ctrl', false , 'GET');
    //$method the method of request for edit or view enum[ view | form | delete | list | page]
    $func  = FormUtil::getPassedValue ('func', 'form' , 'GET');
    //$id the id no if edit form
    $id      = FormUtil::getPassedValue ('id', null , 'GET');
    //pagnigation variable
    $filter  = FormUtil::getPassedValue ('filter', 0);
    $offset  = FormUtil::getPassedValue ('startnum', 0);
    $sort    = FormUtil::getPassedValue ('sort', '');
    $where   = '';
    $step    = FormUtil::getPassedValue ('step', null , 'GET');

    $pagesize = pnModGetVar ('VoteThailandPoll', 'pagesize') ? pnModGetVar ('VoteThailandPoll', 'pagesize') : 100;
    $render = pnRender::getInstance('VoteThailandPoll');
    $mode = null;

    //load class
    if (!($class = Loader::loadClassFromModule ('VoteThailandPoll', $ctrl, false)))
      return LogUtil::registerError ("Unable to load class [$ctrl] ...");

    $object  = new $class ();

    if ($id && $object){

      $object->get($id);

      $mode = 'edit';  
      $render->assign ('form', $object->_objData);
      
    }else{
      $mode = 'new';
    }
    $render->assign ('mode', $mode);
    
    if (method_exists($object,'selectExtendResult')){
      $resultex = $object->selectExtendResult();
      $render->assign('extendResult', $resultex);
    }
    _preRender($render);
    if($step){
      return $render->fetch('admin_'.$func.'_'.strtolower($ctrl).'_step'.$step.'.htm');
    }else {
      return $render->fetch('admin_'.$func.'_'.strtolower($ctrl).'.htm');
    }

}
/**
* display page with class that extend Object Array
*/
function VoteThailandPoll_admin_list() {
    VoteThailandPoll_permission();
    //$ctrl the class name
    $ctrl    = FormUtil::getPassedValue ('ctrl', 'Province' , 'GET');

    //$method the method of request for edit or view enum[ view | form | delete | list | page]
    $func  = FormUtil::getPassedValue ('func', 'list' , 'GET');

    $is_export = false;

    $pagesize = pnModGetVar ('VoteThailandPoll', 'pagesize') ? pnModGetVar ('VoteThailandPoll', 'pagesize') : 100;
    $render = pnRender::getInstance('VoteThailandPoll');

    //check is export
    $export = FormUtil::getPassedValue ('export', false);
    $button_export = FormUtil::getPassedValue ('button_export', false);
    $button_export_x = FormUtil::getPassedValue ('button_export_x', false);
    if ($export || $button_export || $button_export_x){
      $is_export = true;
    }

    $class = Loader::loadClassFromModule ('VoteThailandPoll', $ctrl, true);
    if ($class){
      $objectArray = new $class ();
      $where   = null;
      $sort = null;
      if (method_exists($objectArray,'genFilter')){
        $where = $objectArray->genFilter();
      }
      if (method_exists($objectArray,'genSort')){
        $sort = $objectArray->genSort();
      }
      if (method_exists($objectArray,'selectExtendResult')){
        $resultex = $objectArray->selectExtendResult();
        $render->assign('extendResult', $resultex);
      }
      if (empty($where)){
        $where = $objectArray->_objWhere;
      }
      if (empty($sort)){
        $sort = $objectArray->_objSort;
      }
      //pagnigation variable
      $filter  = FormUtil::getPassedValue ('filter', 0);
      $offset  = FormUtil::getPassedValue ('startnum', 0);
      //$sort    = FormUtil::getPassedValue ('sort', $sort);
      //Split page
      $pagesize = 100;
      $pager = array();
      $pager['numitems']     = $objectArray->getCount ($where , true);
      $pager['itemsperpage'] = $pagesize;
      $render->assign ('startnum', $offset);
      $render->assign ('pager', $pager);
      $objectArray->get ($where, $sort , $offset, $pagesize);
      //assign to view
      $render->assign('objectArray', $objectArray->_objData);
    }
    //_preRender($render);
  
    return $render->fetch('admin_'.$func.'_'.strtolower($ctrl).'.htm');

}

/**
* for delete object for database by specify id
*/
function VoteThailandPoll_admin_delete() {
    VoteThailandPoll_permission();
    $ctrl    = FormUtil::getPassedValue ('ctrl', false , 'GET');
    $id      = FormUtil::getPassedValue ('id', null , 'GET');
    $forward = FormUtil::getPassedValue ('forward', null , 'GET');

    //Get arg
    $pro_id = FormUtil::getPassedValue ('pro_id', null , 'GET');
    $dis_id = FormUtil::getPassedValue ('dis_id', null , 'GET');
    $tam_id = FormUtil::getPassedValue ('tam_id', null , 'GET');

    if ($id){
      //load class
      if (!($class = Loader::loadClassFromModule ('VoteThailandPoll', $ctrl, false)))
        return LogUtil::registerError ("Unable to load class [$ctrl] ...");

      $object  = new $class ();
      $object->_objData[$object->_objField] = $id;
      $object->delete ();

      if($forward){
        $list_url = pnModURL('VoteThailandPoll', 'admin', 'list' , array('ctrl'   => $ctrl,
                                                                       'form[pro_id]' => $pro_id,
                                                                       'form[dis_id]' => $dis_id,
                                                                       'form[tam_id]' => $tam_id
                                                                )
                            );
      }else{
        $list_url = pnModURL('VoteThailandPoll', 'admin', 'list' , array('ctrl'=>$ctrl
                                                                )
                            );
      }

      if (method_exists($object,'genForward')){
        $forwar_url = $object->genForward();
        pnRedirect($forwar_url);
      }else{
        pnRedirect($list_url);
      }
      die;
    }
}

/**
* for set/unset status for hot package
* ex. index.php?module=VoteThailandPoll&func=status&ctrl=VoteThailandPoll&id=XXX&status=YYYY
*/
function VoteThailandPoll_admin_status() {
    VoteThailandPoll_permission();
  //get status from url 
  $id        = FormUtil::getPassedValue ('id', false , 'GET');
  $status    = FormUtil::getPassedValue ('status', false , 'GET');
  $ctrl    = FormUtil::getPassedValue ('ctrl', false , 'GET');
  if ($status && $id){
    //set status
    $result = pnModAPIFunc('VoteThailandPoll', 'admin', 'setStatus',
                         array( 'id'     => $id, 
                                'status' => $status));
  }
  $view_url = pnModURL('VoteThailandPoll', 'admin', 'list' , array('ctrl'=>$ctrl));
  pnRedirect($view_url);
  die;
}


function VoteThailandPoll_admin_changefoodstatus() {
   VoteThailandPoll_permission();
  //get status from url 
  $id      = FormUtil::getPassedValue ('id', false , 'GET');
  $status  = FormUtil::getPassedValue ('status', false , 'GET');
  $ctrl    = FormUtil::getPassedValue ('ctrl', false , 'GET');

  //Get Table
  $pntables = pnDBGetTables();
  $columnFood  = $pntables['ibistro_food_column'];
  $orderArray = array();

  //Change Status
  if($status==1){
    $obj  = array('status' => '2');}
  if($status==2){
    $obj  = array('status' => '1');}
  $where  = "WHERE $columnFood[id]='$id'";
  DBUTil::updateObject ($obj, 'ibistro_food', $where);

  updateXMLFoodStatus($status, $id);

  $view_url = pnModURL('VoteThailandPoll', 'admin', 'list' , array('ctrl'=>$ctrl));
  pnRedirect($view_url);
  die;
}


/**
* display page with class that extend Object 
*/
function VoteThailandPoll_admin_popup() {
    VoteThailandPoll_permission();
    //$ctrl the class name
    $ctrl    = FormUtil::getPassedValue ('ctrl', false , 'GET');
    //$method the method of request for edit or view enum[ view | form | delete | list | page]
    $func  = FormUtil::getPassedValue ('func', 'view' , 'GET');
    //$lang enum[eng | jpn | tha]
    $lang    = FormUtil::getPassedValue ('lang', null , 'GET');
    //$id the id no if edit form
    $id      = FormUtil::getPassedValue ('id', null , 'GET');
    //pagnigation variable
    $filter  = FormUtil::getPassedValue ('filter', 0);
    $offset  = FormUtil::getPassedValue ('startnum', 0);
    $sort    = FormUtil::getPassedValue ('sort', '');
    $where   = '';

    $pagesize = pnModGetVar ('VoteThailandPoll', 'pagesize') ? pnModGetVar ('VoteThailandPoll', 'pagesize') : 100;
    $render = pnRender::getInstance('VoteThailandPoll');
    
    if ($id){
      //load class
      if (!($class = Loader::loadClassFromModule ('VoteThailandPoll', $ctrl, false)))
        return LogUtil::registerError ("Unable to load class [$ctrl] ...");

      $object  = new $class ();
      $object->get($id);
      if (method_exists($object,'selectExtendResult')){
        $resultex = $object->selectExtendResult();
        $render->assign('extendResult', $resultex);
      }
      $render->assign ('view', $object->_objData);
    }
    //_preRender($render);
    echo $render->fetch('admin_'.$func.'_'.strtolower($ctrl).'.htm');
    return true;

    //return $render->fetch('admin_'.$func.'_'.strtolower($ctrl).'.htm');
    //return true;

}




function updateXMLFoodStatus($status, $food_id){
     
     ///////////////////////////////////////////////////////
     //Query for XML
     ///////////////////////////////////////////////////////
     $pntables    = pnDBGetTables();
     $columnFood  = $pntables['ibistro_food_column'];
     $foodArray   = array();
     $sql = "
             SELECT
               $columnFood[id],
               $columnFood[category_id],
               $columnFood[title],
               $columnFood[detail],
               $columnFood[price],
               $columnFood[status],
               $columnFood[imagepath]
             FROM
               $pntables[ibistro_food]
             
              ";

     $column = array('id', 'category_id', 'title', 'detail', 'price', 'status', 'imagepath');

     $result = DBUtil::executeSQL($sql);
     $foodArray = DBUtil::marshallObjects ($result, $column);


     //Gen food
     $xml = new DOMDocument('1.0', 'UTF-8');
     $xml->formatOutput = true;
    
     $xml_node_foods = $xml->createElement("foods");
     $xml->appendChild( $xml_node_foods );
     
     for($i=0;$i<sizeof($foodArray);$i++){
       
        $xml_node_food = $xml->createElement("food");
        $xml_node_food->setAttribute("id", $foodArray[$i]["id"]);
        $xml_node_foods->appendChild( $xml_node_food );

        $xml_node_food_type = $xml->createElement( "type", $foodArray[$i]["category_id"] );
        $xml_node_food->appendChild( $xml_node_food_type );
        $xml_node_food_image = $xml->createElement( "image", "http://192.168.1.5/truck/".$foodArray[$i]["imagepath"]);
        $xml_node_food->appendChild( $xml_node_food_image );
        $xml_node_food_title = $xml->createElement( "title", $foodArray[$i]["title"]);
        $xml_node_food->appendChild( $xml_node_food_title );
        $xml_node_food_status = $xml->createElement( "status", $foodArray[$i]["status"]);
        $xml_node_food->appendChild( $xml_node_food_status );
        $xml_node_food_price = $xml->createElement( "price", $foodArray[$i]["price"] );
        $xml_node_food->appendChild( $xml_node_food_price );

     
     }


      //echo $xml->saveXML();
      $xml->saveXML();
      $xml->save("endpoint/food.xml");

}